package com.changgou.file.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.file.util.FastDFSClient;
import com.changgou.file.util.FastDFSFile;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

@RestController
@RequestMapping("/file")
public class FileController {
    /**
     * 文件上传
     *
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public Result uploadFile(MultipartFile file) {
        try {
            if (file == null) {
                throw new RuntimeException("文件不存在");
            }
            //3.通过MultipartFile 获取FastDFSFile相关参数
            //3.1 文件名
            String originalFilename = file.getOriginalFilename();
            if (StringUtils.isEmpty(originalFilename)) {
                throw new RuntimeException("文件不存在");
            }
            //3.2 文件内容
            byte[] context = file.getBytes();
            //3.3 获取拓展名
            int i = originalFilename.lastIndexOf(".") + 1;
            String extName = originalFilename.substring(i);

            //2.构建fastDFSfile
            FastDFSFile fastDFSFile = new FastDFSFile(originalFilename, context, extName);

            //1 fastdfsclient上传文件
            String[] upload = FastDFSClient.upload(fastDFSFile);
            for (String s : upload) {
                System.out.println(s);
            }
            //upload[0]:组名
            //upload[0]:没有组名的文件名
            String url = FastDFSClient.getTrackerUrl() + upload[0] + "/" + upload[1];

            return new Result(true, StatusCode.OK, "文件上传成功", url);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result(false, StatusCode.ERROR, "文件上传失败");
    }

    //文件下载
    @GetMapping("/downFile/{groupName}/{x}/{y}/{z}/{fileName}")
    public Result downFile(@PathVariable("groupName") String groupName, @PathVariable("fileName") String fileName, @PathVariable("x") String x, @PathVariable("y") String y, @PathVariable("z") String z) throws Exception {

        String remoteFileName = x + "/" + y + "/" + z + "/" + fileName;
        InputStream inputStream = FastDFSClient.downFile(groupName, remoteFileName);
        if (inputStream == null){
            throw new RuntimeException("当前圈中没有该文件");
        }else {
            int len;
            byte[] buffer = new byte[1024];
            OutputStream out = new FileOutputStream(new File("D://bt//" + fileName));
            while ((len = inputStream.read(buffer)) != -1) {
                //将缓冲区的数据输出到客户端浏览器
                out.write(buffer, 0, len);
                out.flush();
            }
            out.close();
            inputStream.close();
            return new Result(true, StatusCode.OK, "文件下载成功");
        }
    }

    //删除文件
    @DeleteMapping("/deleteFile/{groupName}/{x}/{y}/{z}/{fileName}")
    public Result deleteFile(@PathVariable("groupName") String groupName, @PathVariable("fileName") String fileName, @PathVariable("x") String x, @PathVariable("y") String y, @PathVariable("z") String z) throws Exception {
        String remoteFileName = x + "/" + y + "/" + z + "/" + fileName;
        FastDFSClient.deleteFile(groupName, remoteFileName);
        return new Result(true,StatusCode.OK,"文件删除成功");
    }

}
